<template>
  <div id="components-blog-item">
    <div class="new-blog-item" v-for="item in list" :key="item.id">
      <div v-if="item.img" class="img" @click="goBlogdetails(item.id)">
        <img :src="item.img" />
      </div>
      <div :class="['describe', item.img ? 'haveImg' : 'noImg']">
        <h1 class="oneLineDot describe-title" @click="goBlogdetails(item.id)">
          <span
            v-if="$route.name === 'blog-search' || $route.name === 'manage-edit-content'"
            v-html="item.title"
          ></span>
          <span v-else>{{ item.title }}</span>
        </h1>
        <p class="twoLineDot" v-html="item.description">
          <!-- {{ item.description }} -->
        </p>
        <div class="time-views">
          <span class="time">
            <icon-svg name="time"></icon-svg>
            <span> {{ $utils.formatDateTimeYMD(item.createdAt) }}</span>
          </span>
          <span class="views">
            <icon-svg name="views"></icon-svg>
            <span> {{ item.views }}</span>
          </span>
        </div>
      </div>
      <div class="change-blog">
        <span v-if="editOption" class="edit" @click="handleEdit(item.id)">编辑</span>
        <span v-if="deleteOption" class="delete" @click="handleDelete(item.title, item.id)">删除</span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    list: {
      type: Array
    },
    editOption: { type: Boolean, default: false },
    deleteOption: { type: Boolean, default: false }
  },
  methods: {
    goBlogdetails(id) {
      this.$router.push({ name: 'blog-details', params: { id } })
    },
    handleEdit(id) {
      this.$router.push({ name: 'blog-create', query: { articleId: id } })
    },
    handleDelete(title, id) {
      this.$confirm(`确认删除${title}嘛`)
        .then(() => {
          this.$ajax({
            url: this.$ajax.setUrl(`/article/${id}`),
            method: 'delete',
            params: this.$ajax.setParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$emit('refresh')
              this.$message.success('删除成功')
            } else {
              this.$message.error(data.data || data.msg)
            }
          })
        })
        .catch(() => {
          //
        })
    }
  }
}
</script>

<style lang="scss">
#components-blog-item {
  .new-blog-item {
    position: relative;
    display: flex;
    padding: 30px;
    border-bottom: 1px solid rgb(224, 224, 224);
    height: 120px;
    &:hover {
      img {
        transform: scale(1.1);
      }
    }
    .img {
      position: relative;
      width: 200px;
      height: 100%;
      border-radius: 5px;
      overflow: hidden;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        transition: all 0.6s;
        transform: translate(1.1);
      }
      .img-blog-class {
        position: absolute;
        top: 0;
        left: 0;
      }
    }

    .describe {
      position: relative;
      margin-left: 2%;
      height: 100%;

      &.noImg {
        width: 90%;
      }
      &.haveImg {
        width: 70%;
      }

      h1 {
        margin-top: 5px;
        font-size: 1.7rem;
        font-weight: bold;
        color: #333;
        letter-spacing: 2px;
        line-height: 1.2;
        cursor: pointer;

        width: 100%;
        height: 20px;
        &:hover {
          color: #409eff;
        }
      }
      & > p {
        height: 38px;
        overflow: hidden;
        color: #888;
        line-height: 1.3;
        margin: 15px 0;
      }
      .time-views {
        position: absolute;
        color: #888;
        bottom: 5px;
        .views {
          margin-left: 10px;
        }
      }
    }

    .change-blog {
      position: absolute;
      right: 30px;
      bottom: 30px;
      & > span {
        margin-right: 20px;
        font-size: 14px;
        cursor: pointer;
      }
      .edit {
        color: $theme-blue;
      }
      .delete {
        color: $theme-delete;
      }
    }
  }
}
</style>
